package com.cedarhd.brzx.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.util.Map;
import java.util.Properties;

/**
 * @author Evan
 * @date 2018/4/27
 */
public class yamlUtil {

    private static final Logger LOG = LoggerFactory.getLogger(yamlUtil.class);

    /**
     * yaml转map
     * @param yamlSource
     * @return
     */
    public static Map<String, Object> yaml2Map(String yamlSource) {
        try {
            YamlMapFactoryBean yaml = new YamlMapFactoryBean();
            yaml.setResources(new ClassPathResource(yamlSource));
            return yaml.getObject();
        } catch (Exception e) {
            LOG.error("Cannot read yaml", e);
            return null;
        }
    }

    /**
     * yaml转properties
     * @param yamlSource
     * @return
     */
    public static Properties yaml2Properties(String yamlSource) {
        try {
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ClassPathResource(yamlSource));
            return yaml.getObject();
        } catch (Exception e) {
            LOG.error("Cannot read yaml", e);
            return null;
        }
    }
}
